package Day24;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;

public class Demo01 {
    private Socket socket;
    private View v = new View();

    public static void main(String[] args) throws IOException {
        Demo01 d = new Demo01();
        d.link();
    }


    //创建与服务端对接的客户端,菜单界面
    public void link() throws IOException {
        OutputStream os = null;//全部初始化
        InputStream is = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try{
            socket = new Socket("localhost",65535);
            is = socket.getInputStream();
            os = socket.getOutputStream();
            oos = new ObjectOutputStream(os);
            ois = new ObjectInputStream(is);
            aa:while(true){
                int a = v.xxx();
                oos.writeInt(a);
                switch (a){
                    case 1:{
                        gClient(oos,ois);
                        break;
                    }
                    case 2:{
                        uClient(oos,ois);
                        break;
                    }
                    case 0:{
                        break aa;
                    }
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(ois != null) {
                ois.close();
            }
            if(oos != null) {
                oos.close();
            }
            if(socket != null) {
                socket.close();
            }

        }
    }

    //管理员界面
    public void gClient(ObjectOutputStream oos,ObjectInputStream ois) throws IOException, ClassNotFoundException {
        while(true){
            int a = v.courier();
            oos.writeInt(a);
            switch (a){
                case 1:{
                    add(oos,ois);
                    break;
                }
                case 2:{
                    delete(oos,ois);
                    break;
                }
                case 3:{
                    revise(oos,ois);
                    break;
                }
                case 4:{
                    printAll(ois);
                    break;
                }
                case 0:{
                    return;
                }
            }
        }
    }

    //用户界面
    public void uClient(ObjectOutputStream oos,ObjectInputStream ois) throws IOException, ClassNotFoundException {
        int code = v.user();
        oos.writeObject(code);
        oos.flush();
        Express e = (Express) ois.readObject();
        if(e != null){
            v.printExpress(e);
            if(ois.readBoolean()) {
                v.success();
            }
        }else{
            v.fail();
        }
    }

    //添加快递
    public void add(ObjectOutputStream oos,ObjectInputStream ois) throws IOException, ClassNotFoundException {
        Express e = v.add();
        oos.writeObject(e);
        oos.flush();
        Express e1 = (Express) ois.readObject();
        if(e1 == null){
            if(ois.readBoolean()) {
                v.success();
            }
        }else{
            v.fail();
        }
    }

    //删除快递
    public void delete(ObjectOutputStream oos,ObjectInputStream ois) throws IOException, ClassNotFoundException {
        String number = v.delete();
        oos.writeObject(number);
        oos.flush();
        Express e = (Express) ois.readObject();
        v.printExpress(e);
        if(ois.readBoolean()) {
            v.success();
        }else{
            v.fail();
        }
    }

    //修改快递
    public void revise(ObjectOutputStream oos,ObjectInputStream ois) throws IOException, ClassNotFoundException {
        String s = v.delete();
        oos.writeObject(s);
        oos.flush();
        Express e = (Express) ois.readObject();
        if(e != null){
            Express e1 = v.revise();
            oos.writeObject(e1);
            oos.flush();
            if(ois.readBoolean() && ois.readBoolean()){
                v.success();
            }else{
                v.fail();
            }
        }else{
            System.out.println("没有该快递信息");
        }
    }


    //遍历快递
    public void printAll(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
        v.printfAll(list);
    }
}
